import React, { useState, useContext, useEffect } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { Table, Button, Modal } from "antd";
import { Observer } from "mobx-react";

import http from "../../../../../services/service";
import { Context } from "../../../../../store/MyContext";

import Tab from "../Tabs/Tabs";

function UpdateModal(props) {
  const { allSingleSet, chinaCity, alOrganization } = props;
  const context = useContext(Context);
  const [tableData, setTableData] = useState([]);

  //表单数据
  const [addNewSetData, setAddNewSetData] = useState({});
  const [addSetItem, setAddSetItem] = useState([]);

  const [confirmLoading, setConfirmLoading] = useState(false);
  const [modalText, setModalText] = useState("Content of the modal");

  useEffect(() => {});

  const formObj = {
    // com_proName: addNewSetData.user.com_proName,
    // com_UID: addNewSetData.user.com_UID,
    // com_originalPrice: addNewSetData.user.com_originalPrice,
    // com_presentPrice: addNewSetData.user.com_presentPrice,
    // com_address: addNewSetData.user.com_address,
    // com_label: addNewSetData.user.com_label,
    // org_name: addNewSetData.user.org_name,
    // com_status: addNewSetData.user.com_status,
    // com_introduce: addNewSetData.user.com_introduce,
    // com_rules: addNewSetData.user.com_rules,
    // startTimeName: addNewSetData.user.startTimeName,
    // endTimeName: addNewSetData.user.endTimeName,
    // phy_id: addSetItem.join(","),
  };

  const handleOk = () => {
    // setModalText("The modal will be closed after two seconds");
    // setConfirmLoading(true);
    // setTimeout(() => {
    //   // setVisible(false);
    //   context.showModal();
    //   setConfirmLoading(false);
    // }, 2000);
    console.log("添加确定按钮点击");

    http.post("/api1/addSet", { ...formObj }).then((response) => {
      // message.loading({ content: "正在添加...", key });
      // setTimeout(() => {
      //   message.success({ content: "已完成!", key, duration: 2 });
      // }, 100);
      // getAllSingleSet();
      // context.showModal();
      // form.resetFields();
      console.log("添加确定按钮点击");
    });
  };

  //第一个页面的表单数据
  const onfirstFormSubmit = (formItem1) => {
    let startTimeName = "startTimeName";
    let endTimeName = "endTimeName";
    console.log(333, formItem1.user.createTime);
    console.log(111, formItem1.user.createTime[0]._d);
    let startTimeOri = formItem1.user.createTime[0]._d;
    let endTimeOri = formItem1.user.createTime[1]._d;
    let startTime = fromData(startTimeOri);
    let endTime = fromData(endTimeOri);
    formItem1.user[startTimeName] = startTime;
    formItem1.user[endTimeName] = endTime;
    console.log(666, startTime, endTime, formItem1);
    console.log(999999, addNewSetData);

    setAddNewSetData(formItem1);
  };

  //第二个页面的表单数据
  const onsecondFormSubmit = (formItem2) => {
    console.log(222, formItem2);
    setAddSetItem(formItem2);
  };

  //时间戳处理函数，时间戳转换成正常数据
  let fromData = (a) => {
    let time = new Date(a);
    let month = time.getMonth() + 1;
    let date = time.getDate();
    let hours = time.getHours();
    let minutes = time.getMinutes();
    // let second = time.getSeconds() // 获取秒数
    // 小于10前加一个0
    if (month < 10) {
      month = "0" + month;
    }
    if (date < 10) {
      date = "0" + date;
    }
    return time.getFullYear() + "-" + month + "-" + date;
  };

  const handleCancel = () => {
    console.log("Clicked cancel button");
    context.showModal();
  };

  return (
    <Observer>
      {() => (
        <Modal
          title='新增体检套餐'
          visible={context.isVisible}
          // visible={true}
          onOk={handleOk}
          confirmLoading={confirmLoading}
          onCancel={handleCancel}
          width={1200}
        >
          <Tab
            allSingleSet={allSingleSet}
            onfirstFormSubmit={onfirstFormSubmit}
            onsecondFormSubmit={onsecondFormSubmit}
            chinaCity={chinaCity}
            alOrganization={alOrganization}
          ></Tab>
        </Modal>
      )}
    </Observer>
  );
}

export default UpdateModal;
